package com.lipl.base.javase.juc.demo;

import java.util.concurrent.atomic.AtomicReference;

//不可重入锁：以上面的代码实例来说明，就是methodA进入methodB的时候不能直接获取锁，必须先调用unLock释放锁。
// 才能执行下去，那实现不可重入锁有什么方式呢？那就是自旋，所以会有一个小名叫做自旋锁。​​​​​​​
public class SpinLock {

  private AtomicReference<Thread> sign =new AtomicReference<>();

  public void lock(){
    Thread current = Thread.currentThread();
    while(!sign .compareAndSet(null, current)){
    }
  }

  public void unlock (){
    Thread current = Thread.currentThread();
    sign .compareAndSet(current, null);
  }
}